package com.news.news.service;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.news.news.dao.UserCenterDao;
import com.news.news.modle.User;
import com.news.news.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Transactional
public class UserCenterService {

    @Resource
    UserCenterDao userCenterDao;

    public User information(String userToken) {
        //通过userToken 拿到user的id
        DecodedJWT tokenInfo = JWTUtil.getTokenInfo(userToken);
        int id = tokenInfo.getClaim("id").asInt();
        User user = userCenterDao.information(id);
        return user;
    }

    public void update(User user) {
        userCenterDao.update(user);
    }

    public String xzOldPsw(String userToken, String password) {
        //通过userToken 拿到user的id
        DecodedJWT tokenInfo = JWTUtil.getTokenInfo(userToken);
        int id = tokenInfo.getClaim("id").asInt();
        User user = userCenterDao.xzOldPsw(id,password);
          if (user==null){
              return "密码错误！!!!!";
          }else {
              return "密码正确！";
          }
    }

    public void saveNewPsw(String userToken, String password) {
        DecodedJWT tokenInfo = JWTUtil.getTokenInfo(userToken);
        int id = tokenInfo.getClaim("id").asInt();
        userCenterDao.saveNewPsw(id,password);
    }
}
